/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Call activity calledElement type property
 */

angular.module('flowableModeler').controller('FlowableCalledElementTypeCtrl', [
  '$scope',
  function($scope) {
    if ($scope.property.value == undefined && $scope.property.value == null) {
      $scope.property.value = 'key'
    }

    $scope.calledElementTypeChanged = function() {
      $scope.updatePropertyInModel($scope.property)
    }
  }
])

angular.module('flowableModeler').controller('FlowableCollectionCtrl', [
  '$scope',
  '$modal',
  function($scope,$modal) {
    var opts = {
      template: 'editor-app/configuration/properties/collection-popup.html?version=' + Date.now(),
      scope: $scope
    }
    _internalCreateModal(opts, $modal, $scope)
  }
])

angular.module('flowableModeler').controller('FlowableCollectionPopupCtrl', [
  '$rootScope',
  '$scope',
  '$translate',
  '$http',
  '$timeout',
  'UserService',
  function($rootScope, $scope, $translate, $http, $timeout,UserService){
    //集合名称
    $scope.popup = {}
    if($scope.property.value){
      $scope.popup.collection = $scope.property.value
    }else{
      $scope.popup.collection = ''
    }
    //集合子集
    $scope.popupCollectionValue =  $scope.collectionValue.value || []
    //监控输入框
    $scope.$watch('popup.filter', function() {
      $scope.updateFilter()
    })
    //获取搜索数据
    $scope.updateFilter = function() {
      if($scope.popup.oldFilter == undefined || $scope.popup.oldFilter != $scope.popup.filter) {
        if(!$scope.popup.filter) {
          $scope.popup.oldFilter = ''
        }else {
          $scope.popup.oldFilter = $scope.popup.filter
        }

        if($scope.popup.filter !== null && $scope.popup.filter !== undefined) {
          UserService.getFilteredUsersCRC($scope.popup.filter).then(function(res) {
            $scope.popup.userResults = res.records
            $scope.resetSelection()
          })
        }
      }
    }
    //重置默认加灰
    $scope.resetSelection = function() {
      if ($scope.popup.userResults && $scope.popup.userResults.length > 0) {
        $scope.popup.selectedIndex = 0
      } else {
        $scope.popup.selectedIndex = -1
      }
    }
    $scope.nextUser = function() {
      var users = $scope.popup.userResults
      if (users && users.length > 0 && $scope.popup.selectedIndex < users.length - 1) {
        $scope.popup.selectedIndex += 1
      }
    }
    $scope.previousUser = function() {
      var users = $scope.popup.userResults
      if (users && users.length > 0 && $scope.popup.selectedIndex > 0) {
        $scope.popup.selectedIndex -= 1
      }
    }
    $scope.confirmUser =  function(user) {
      if (!user) {
        var users = $scope.popup.userResults
        if ($scope.popup.selectedIndex >= 0 && $scope.popup.selectedIndex < users.length) {
          user = users[$scope.popup.selectedIndex]
        }
      }
      if(user){
        var isNewUser = $scope.popupCollectionValue.some(function(item){
          return item.id == user.id
        })
        if(!isNewUser){
          $scope.popupCollectionValue.push(user)
        }
      }
    }
    $scope.removeUser = function(user){
      var users = $scope.popupCollectionValue
      $scope.popupCollectionValue = users.filter(function(item){
        return item.id != user.id
      })
    }

    $scope.close = function() {
      $scope.property.mode = 'read'
      $scope.$hide()
    }

    $scope.save = function() {
      //设置集合名称为必填
      if(!$scope.popup.collection){
        $rootScope.alerts['current'] = {
          type: 'alert-danger',
          message: $translate.instant('GENERAL.MESSAGE.COLLECTION-NAME')
        }
        $timeout(function() {
          $rootScope.alerts['current'] = ''
        }, 3000)
        return false
      }
      //设置集合value
      $scope.property.value = $scope.popup.collection
      $scope.collectionValue.value = $scope.popupCollectionValue
      $scope.updatePropertyInModel($scope.property)
      $scope.updatePropertyInModel($scope.collectionValue)
      $scope.close()
    }
  }
])
